package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.RedisConstant;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.SMSUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;


import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${out_put_path}")
    private String outPutPath;
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //添加套餐组
        setMealMapper.add(setmeal);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        //添加中间表t_setmeal_checkgroup
        addSetMealIdAndCheckgroupId(setmeal.getId(),checkgroupIds);
        generateMobileStaticHtml();
    }
    public void generateMobileStaticHtml(){
        List<Setmeal> list = this.getSetmeal();
        generateMobileSetmealListHtml(list);
        generateMobileSetmealDetailHtml(list);
    }

    public void generateMobileSetmealListHtml(List<Setmeal> setmealList){
        Map map=new HashMap();
        map.put("setmealList",setmealList);
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }
    public void  generateMobileSetmealDetailHtml(List<Setmeal> setmealList){
        for (Setmeal setmeal : setmealList) {
            Map map=new HashMap();
            map.put("setmeal",this.findById(setmeal.getId()));
            generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
        }


    }
    public void generateHtml(String templateName,String fileName,Map map){
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Template template = null;
        try {
            template = configuration.getTemplate(templateName);

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outPutPath + "/" + fileName), "UTF-8"));
//            FileWriter fileWriter = new FileWriter(new File(outPutPath+"/"+fileName));
            template.process(map,bufferedWriter);
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if(queryString != null && queryString.length()>0){
            queryString="%"+queryString+"%";
        }
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page=setMealMapper.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return setMealMapper.getSetmeal();
    }

    @Override
    public Setmeal findById(Integer id) {
        return setMealMapper.findById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setMealMapper.findSetmealCount();
    }


    public void addSetMealIdAndCheckgroupId(Integer setmeal_id,Integer[] checkgroupIds){
        if(checkgroupIds != null && checkgroupIds.length>0){
            for (Integer checkgroup_id : checkgroupIds) {
                Map<String,Integer> map=new HashMap<String,Integer>();
                map.put("setmeal_id",setmeal_id);
                map.put("checkgroup_id",checkgroup_id);
                setMealMapper.addSetMealIdAndCheckgroupId(map);
            }
        }
    }
}